/*
 * Copyright (C) Jahia Ltd. All rights reserved.
 *
 * This software is published under the terms of the Jahia Open Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 */
package org.jahia.sqlprofiler;

import java.util.*;

/**
 * <p>Title: Stores the result of a parsed SQL Select statement, storing the
 * table and columns names associated with the query.</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Jahia Ltd</p>
 * @author Serge Huber
 * @version 1.0
 */

public class QueryStatEntry implements Comparable {
    private ArrayList tableNames = new ArrayList();
    private ArrayList columnNames = new ArrayList();
    private Set queries = new TreeSet();
    private int occurences = 0;
    private long totalElapsedTime = 0;

    public QueryStatEntry() {
    }

    public QueryStatEntry(ArrayList tableNames, ArrayList columnNames) {
        // we copy here to make sure we have only one value for each table and
        // column name, which is not guaranteed since we are using ArrayList
        // to preserve order of the detected identifiers.
        Iterator tableNameIter = tableNames.iterator();
        while (tableNameIter.hasNext()) {
            String curTableName = (String) tableNameIter.next();
            if (!this.tableNames.contains(curTableName)) {
                this.tableNames.add(curTableName);
            }
        }
        Iterator columnNameIter = columnNames.iterator();
        while (columnNameIter.hasNext()) {
            String curColumnName = (String) columnNameIter.next();
            if (!this.columnNames.contains(curColumnName)) {
                this.columnNames.add(curColumnName);
            }
        }
    }
    public java.util.ArrayList getTableNames() {
        return tableNames;
    }
    public java.util.ArrayList getColumnNames() {
        return columnNames;
    }
    public int getOccurences() {
        return occurences;
    }

    public long getTotalElapsedTime() {
        return totalElapsedTime;
    }

    public void incOccurences() {
        occurences++;
    }

    public void incTotalElapseTime(long elapsedTime) {
        totalElapsedTime += elapsedTime;
    }

    public void addQuery(QueryEntry query) {
        queries.add(query);
    }

    public Set getQueries() {
        return queries;
    }

    public String getKey() {
        String hashCodeStr = tableNames.toString() + "_" + columnNames.toString();
        return hashCodeStr;
    }

    public int hashCode() {
        return getKey().hashCode();
    }

    public boolean equals(Object o) {
        if (o instanceof QueryStatEntry) {
            QueryStatEntry right = (QueryStatEntry) o;
            return getKey().equals(right.getKey());
        } else {
            return false;
        }
    }

    public int compareTo(Object o) throws ClassCastException {
        QueryStatEntry right = (QueryStatEntry) o;

        // first let's compare total time.
        Long leftElapsedTime = new Long(getTotalElapsedTime());
        Long rightElapsedTime = new Long(right.getTotalElapsedTime());
        int compareElapsed = -leftElapsedTime.compareTo(rightElapsedTime);
        if (compareElapsed != 0) {
            return compareElapsed;
        }

        Integer left = new Integer(occurences);
        return -left.compareTo(new Integer(right.getOccurences()));
    }

}